package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.UserMapper;
import cn.tedu.egcar1.pojo.dto.UserRegParam;
import cn.tedu.egcar1.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/v1/user/")
@Slf4j
@Api(tags = "01.用户管理模块")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @ApiOperation(value = "用户注册")
    @PostMapping("reg")
    public JsonResult reg(@RequestBody UserRegParam userRegParam){
        log.debug("userRegParam:-------------------------"+userRegParam);
        String username = userRegParam.getUsername();
        User userTemp = userMapper.selectByUsername(username);
        if(userTemp!=null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user:-------------------------"+user);
        int num = userMapper.insert(user);
        if(num>0) return JsonResult.ok();
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

}
